
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Core;

namespace Reservations
{
	public class ReservationBackupBizRules : Syrinx.BaseBizRules
	{
		public void backupRoomAllocations(IApplication app, IRuleContext context, IBizObj deployInfo, ZipOutputStream zOut)
		{
			try
			{
				foreach (IBizObj company in deployInfo.get("backupCompanies") as IBizObjList)
				{
					context.put("companyId", company.get("id"));
					IResults rc = context.RuleGroup.execute(context, "getBookAgentsWithDefaultRoomAllocations", company.get("id"));
					IBizObjList roomAlloc = rc.CallResults as IBizObjList;
					if (roomAlloc != null)
					{
						zOut.PutNextEntry(new ZipEntry(string.Format("data{0}{1}{0}RoomAllocations.xml", Path.DirectorySeparatorChar, company.get("shortName"))));
						TextWriter t = (TextWriter)context.get("zipWriter");
						t.Write("<?xml version=\"1.0\" ?>\n" + roomAlloc.Xml.ToString());
						t.Flush();
					}

					rc = context.RuleGroup.execute(context, "getRoomAllocationOverrides", company.get("id"));
					roomAlloc = rc.CallResults as IBizObjList;
					if (roomAlloc != null)
					{
						zOut.PutNextEntry(new ZipEntry(string.Format("data{0}{1}{0}RoomAllocationOverrides.xml", Path.DirectorySeparatorChar, company.get("shortName"))));
						TextWriter t = (TextWriter)context.get("zipWriter");
						t.Write("<?xml version=\"1.0\" ?>\n" + roomAlloc.Xml.ToString());
						t.Flush();
					}
				}
			}
			catch (Exception e)
			{
				throw new BaseException("error executing backupCheckoutWizards.", e);
			}
		}

		public void backupSeasonalPricing(IApplication app, IRuleContext context, IBizObj deployInfo, ZipOutputStream zOut)
		{
			try
			{
				foreach (IBizObj company in deployInfo.get("backupCompanies") as IBizObjList)
				{
					context.put("companyId", company.get("id"));
					IResults rc = context.RuleGroup.execute(context, "getCompanyRoomTypeSeasonalRates", company.get("id"));
					IBizObjList seasonRates = rc.CallResults as IBizObjList;
					if (seasonRates != null)
					{
						zOut.PutNextEntry(new ZipEntry(string.Format("data{0}{1}{0}RoomSeasonalRates.xml", Path.DirectorySeparatorChar, company.get("shortName"))));
						TextWriter t = (TextWriter)context.get("zipWriter");
						t.Write("<?xml version=\"1.0\" ?>\n" + seasonRates.Xml.ToString());
						t.Flush();
					}
				}
			}
			catch (Exception e)
			{
				throw new BaseException("error executing backupCheckoutWizards.", e);
			}
		}

		public void backupReservations(IApplication app, IRuleContext context, IBizObj deployInfo, ZipOutputStream zOut)
		{
		}

		public void postExtendReservationExecuteCompanyBackup(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{

			try
			{
				INameAddressSupport deployInfo = inputParams[2] as INameAddressSupport;
				ZipOutputStream zOut = inputParams[3] as ZipOutputStream;
				IResults rc = null;
				bool fullBackup = (bool)deployInfo.get("fullCompanyBackup");

				if (fullBackup || (bool)deployInfo.get("backupRoomAllocations"))
					rc = context.RuleGroup.execute(context, "backupRoomAllocations", deployInfo, zOut);
				if (fullBackup || (bool)deployInfo.get("backupSeasonalPricing"))
					rc = context.RuleGroup.execute(context, "backupSeasonalPricing", deployInfo, zOut);
				if (fullBackup || (bool)deployInfo.get("backupReservations"))
					rc = context.RuleGroup.execute(context, "backupReservations", deployInfo, zOut);
			}
			catch (Exception e)
			{
				throw new BaseException("error executing postExtendEComexecuteCompanyBackup.", e);
			}
		}

		public object getReservationBackupRestoreTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			string template = "Reservations.Gui.AspNet.BaseDeploymentOptions";
			if (resultValue != null)
				return resultValue.ToString().Length == 0 ? template
					: new StringBuilder(50).Append(resultValue.ToString()).Append(",").Append(template).ToString();
			return template;
		}

	}
}
